#!/bin/bash
# 向文件中写入字符串
# $1 文件名
# $2 sudoCreate sudoAppend create append
# $3 content
function echoFile() {
    case "$2" in
    "sudoCreate")
        sudo sh -c "echo \"$3\" > $1"
        ;;
    "sudoAppend")
        sudo sh -c "echo \"$3\" >> $1"
        ;;
    "create")
        echo "$3" >$1
        ;;
    "append")
        echo "$3" >>$1
        ;;
    *)
        echo "非法的echoMode，echoMode只能为sudoCreate/sudoAppend/create/append中的一种"
        exit -1
        ;;
    esac
}

function echoInLine() {
    echo "==================== " $1 " ===================="
}

function initTmux() {
    echoFile ~/.tmux.conf create '# 代码中设置'
    echoFile ~/.tmux.conf append '# alt+kjhl 切换pane'
    echoFile ~/.tmux.conf append 'bind-key -n M-k select-pane -U'
    echoFile ~/.tmux.conf append 'bind-key -n M-j select-pane -D'
    echoFile ~/.tmux.conf append 'bind-key -n M-h select-pane -L'
    echoFile ~/.tmux.conf append 'bind-key -n M-l select-pane -R'
    echoFile ~/.tmux.conf append ''
    echoFile ~/.tmux.conf append '# alt+io 分割pane'
    echoFile ~/.tmux.conf append 'bind-key -n M-o split-window -h'
    echoFile ~/.tmux.conf append 'bind-key -n M-i split-window -v'
    echoFile ~/.tmux.conf append ''
    echoFile ~/.tmux.conf append '# 设置鼠标可以选择pane以及上下滚动'
    echoFile ~/.tmux.conf append 'set -g mouse on'
    echoFile ~/.tmux.conf append 'set -g status off # 全局隐藏状态栏，如果要多个窗口，直接使用terminal的多窗口即可'
    echoFile ~/.tmux.conf append '# 系统默认配置'
    echoFile ~/.tmux.conf append '# Ctl+b c  create window'
    echoFile ~/.tmux.conf append '# Ctl+b w  list windows'
    echoFile ~/.tmux.conf append '# Ctl+b n  next window'
    echoFile ~/.tmux.conf append '# Ctl+b p  previous window'
    echoFile ~/.tmux.conf append '# Ctl+b f  find window'
    echoFile ~/.tmux.conf append '# Ctl+b ,  name window'
    echoFile ~/.tmux.conf append '# Ctl+b &  kill window'

    echo "tmux初始化完毕，~/.tmux.conf文件内容如下："
    cat ~/.tmux.conf
}

function initAptSource() {
    case "$1" in
    "ubuntu-20.04")
        echoFile /etc/apt/sources.list "sudoAppend" 'deb http://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse'
        echoFile /etc/apt/sources.list "sudoAppend" 'deb-src http://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse'
        echoFile /etc/apt/sources.list "sudoAppend" 'deb http://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse'
        echoFile /etc/apt/sources.list "sudoAppend" 'deb-src http://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse'
        echoFile /etc/apt/sources.list "sudoAppend" 'deb http://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiverse'
        echoFile /etc/apt/sources.list "sudoAppend" 'deb-src http://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiverse'
        echoFile /etc/apt/sources.list "sudoAppend" '# deb http://mirrors.aliyun.com/ubuntu/ focal-proposed main restricted universe multiverse'
        echoFile /etc/apt/sources.list "sudoAppend" '# deb-src http://mirrors.aliyun.com/ubuntu/ focal-proposed main restricted universe multiverse'
        echoFile /etc/apt/sources.list "sudoAppend" 'deb http://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse'
        echoFile /etc/apt/sources.list "sudoAppend" 'deb-src http://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse'
        ;;
    "ubuntu-18.04")
        echoFile /etc/apt/sources.list "sudoAppend" 'deb http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse'
        echoFile /etc/apt/sources.list "sudoAppend" 'deb-src http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse'
        echoFile /etc/apt/sources.list "sudoAppend" 'deb http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse'
        echoFile /etc/apt/sources.list "sudoAppend" 'deb-src http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse'
        echoFile /etc/apt/sources.list "sudoAppend" 'deb http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse'
        echoFile /etc/apt/sources.list "sudoAppend" 'deb-src http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse'
        echoFile /etc/apt/sources.list "sudoAppend" '# deb http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse'
        echoFile /etc/apt/sources.list "sudoAppend" '# deb-src http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse'
        echoFile /etc/apt/sources.list "sudoAppend" 'deb http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse'
        echoFile /etc/apt/sources.list "sudoAppend" 'deb-src http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse'
        ;;
    "ubuntu-16.04")
        echoFile /etc/apt/sources.list "sudoAppend" 'deb http://mirrors.aliyun.com/ubuntu/ xenial main'
        echoFile /etc/apt/sources.list "sudoAppend" 'deb-src http://mirrors.aliyun.com/ubuntu/ xenial main'
        echoFile /etc/apt/sources.list "sudoAppend" 'deb http://mirrors.aliyun.com/ubuntu/ xenial-updates main'
        echoFile /etc/apt/sources.list "sudoAppend" 'deb-src http://mirrors.aliyun.com/ubuntu/ xenial-updates main'
        echoFile /etc/apt/sources.list "sudoAppend" 'deb http://mirrors.aliyun.com/ubuntu/ xenial universe'
        echoFile /etc/apt/sources.list "sudoAppend" 'deb-src http://mirrors.aliyun.com/ubuntu/ xenial universe'
        echoFile /etc/apt/sources.list "sudoAppend" 'deb http://mirrors.aliyun.com/ubuntu/ xenial-updates universe'
        echoFile /etc/apt/sources.list "sudoAppend" 'deb-src http://mirrors.aliyun.com/ubuntu/ xenial-updates universe'
        echoFile /etc/apt/sources.list "sudoAppend" 'deb http://mirrors.aliyun.com/ubuntu/ xenial-security main'
        echoFile /etc/apt/sources.list "sudoAppend" 'deb-src http://mirrors.aliyun.com/ubuntu/ xenial-security main'
        echoFile /etc/apt/sources.list "sudoAppend" 'deb http://mirrors.aliyun.com/ubuntu/ xenial-security universe'
        echoFile /etc/apt/sources.list "sudoAppend" 'deb-src http://mirrors.aliyun.com/ubuntu/ xenial-security universe'
        ;;
    "debian-11")
        echoFile /etc/apt/sources.list "sudoAppend" 'deb http://mirrors.aliyun.com/debian/ bullseye main non-free contrib'
        echoFile /etc/apt/sources.list "sudoAppend" 'deb-src http://mirrors.aliyun.com/debian/ bullseye main non-free contrib'
        echoFile /etc/apt/sources.list "sudoAppend" 'deb http://mirrors.aliyun.com/debian-security/ bullseye-security main'
        echoFile /etc/apt/sources.list "sudoAppend" 'deb-src http://mirrors.aliyun.com/debian-security/ bullseye-security main'
        echoFile /etc/apt/sources.list "sudoAppend" 'deb http://mirrors.aliyun.com/debian/ bullseye-updates main non-free contrib'
        echoFile /etc/apt/sources.list "sudoAppend" 'deb-src http://mirrors.aliyun.com/debian/ bullseye-updates main non-free contrib'
        echoFile /etc/apt/sources.list "sudoAppend" 'deb http://mirrors.aliyun.com/debian/ bullseye-backports main non-free contrib'
        echoFile /etc/apt/sources.list "sudoAppend" 'deb-src http://mirrors.aliyun.com/debian/ bullseye-backports main non-free contrib'
        ;;
    "debian-10")
        echoFile /etc/apt/sources.list "sudoAppend" 'deb http://mirrors.aliyun.com/debian/ buster main non-free contrib'
        echoFile /etc/apt/sources.list "sudoAppend" 'deb-src http://mirrors.aliyun.com/debian/ buster main non-free contrib'
        echoFile /etc/apt/sources.list "sudoAppend" 'deb http://mirrors.aliyun.com/debian-security buster/updates main'
        echoFile /etc/apt/sources.list "sudoAppend" 'deb-src http://mirrors.aliyun.com/debian-security buster/updates main'
        echoFile /etc/apt/sources.list "sudoAppend" 'deb http://mirrors.aliyun.com/debian/ buster-updates main non-free contrib'
        echoFile /etc/apt/sources.list "sudoAppend" 'deb-src http://mirrors.aliyun.com/debian/ buster-updates main non-free contrib'
        echoFile /etc/apt/sources.list "sudoAppend" 'deb http://mirrors.aliyun.com/debian/ buster-backports main non-free contrib'
        echoFile /etc/apt/sources.list "sudoAppend" 'deb-src http://mirrors.aliyun.com/debian/ buster-backports main non-free contrib'
        ;;
    "debian-9")
        echoFile /etc/apt/sources.list "sudoAppend" 'deb http://mirrors.aliyun.com/debian/ stretch main non-free contrib'
        echoFile /etc/apt/sources.list "sudoAppend" 'deb-src http://mirrors.aliyun.com/debian/ stretch main non-free contrib'
        echoFile /etc/apt/sources.list "sudoAppend" 'deb http://mirrors.aliyun.com/debian-security stretch/updates main'
        echoFile /etc/apt/sources.list "sudoAppend" 'deb-src http://mirrors.aliyun.com/debian-security stretch/updates main'
        echoFile /etc/apt/sources.list "sudoAppend" 'deb http://mirrors.aliyun.com/debian/ stretch-updates main non-free contrib'
        echoFile /etc/apt/sources.list "sudoAppend" 'deb-src http://mirrors.aliyun.com/debian/ stretch-updates main non-free contrib'
        echoFile /etc/apt/sources.list "sudoAppend" '#deb http://mirrors.aliyun.com/debian/ stretch-backports main non-free contrib'
        echoFile /etc/apt/sources.list "sudoAppend" '#deb-src http://mirrors.aliyun.com/debian/ stretch-backports main non-free contrib'
        ;;

    "debian-8")
        echoFile /etc/apt/sources.list "sudoAppend" 'deb http://mirrors.aliyun.com/debian/ jessie main non-free contrib'
        echoFile /etc/apt/sources.list "sudoAppend" 'deb http://mirrors.aliyun.com/debian/ jessie-proposed-updates main non-free contrib'
        echoFile /etc/apt/sources.list "sudoAppend" 'deb-src http://mirrors.aliyun.com/debian/ jessie main non-free contrib'
        echoFile /etc/apt/sources.list "sudoAppend" 'deb-src http://mirrors.aliyun.com/debian/ jessie-proposed-updates main non-free contrib'
        ;;
    "debian-7")
        echoFile /etc/apt/sources.list "sudoAppend" 'deb http://mirrors.aliyun.com/debian-archive/debian/ wheezy main non-free contrib'
        echoFile /etc/apt/sources.list "sudoAppend" 'deb http://mirrors.aliyun.com/debian-archive/debian/ wheezy-proposed-updates main non-free contrib'
        echoFile /etc/apt/sources.list "sudoAppend" 'deb-src http://mirrors.aliyun.com/debian-archive/debian/ wheezy main non-free contrib'
        echoFile /etc/apt/sources.list "sudoAppend" 'deb-src http://mirrors.aliyun.com/debian-archive/debian/ wheezy-proposed-updates main non-free contrib'
        ;;
    *)
        echo "不支持的ubuntu版本 ${ID}-${VERSION_ID}"
        ;;
    esac

}

function initYumSource() {
	echo "not implemented"
}

function initSoftwareSource() {
    source /etc/os-release
    echo "当前系统版本为：${ID}-${VERSION_ID}"

    case "$ID-${VERSION_ID}" in
    "ubuntu-20.04" | "ubuntu-18.04" | "ubuntu-16.04")
        sudo cp /etc/apt/sources.list /etc/apt/sources.list_$(date "+%Y%m%d%H%M%S")
        echoFile /etc/apt/sources.list "sudoCreate" "#脚本自动配置aliyun镜像：https://developer.aliyun.com/mirror/ubuntu?spm=a2c6h.13651102.0.0.3e221b11QJ1Ag1"
        initAptSource "${ID}-${VERSION_ID}"
        echo "/etc/apt/sources.list配置完毕,文件内容如下："
        cat /etc/apt/sources.list
        sudo apt update
        ;;

    *)
        echo "未知的操作系统版本 ${ID}-${VERSION_ID}, 程序退出"
        ;;
    esac
}

function main() {
    while true; do
        echo "脚本功能列表如下："
        echo "1. 初始化tmux配置文件（不自动安装tmux）"
        echo "2. 初始化apt/yum软件源"
        echo "*. 其他任意按键退出程序"
        echo -n "请选择功能[1-2]:"
        read fun

        case "${fun}" in
        1)
            echoInLine "初始化tmux"
            initTmux
            exit 0
            ;;
        2)
            echoInLine "初始化软件源"
            initSoftwareSource
            ;;
        *)
            exit 0
            ;;
        esac
    done
}

main
